/*
 * @Description: 对称加密
 * @version: 
 * @Author: 171
 * @Date: 2022-09-04 18:12:09
 * @LastEditors: 171
 * @LastEditTime: 2022-09-22 17:20:58
 */
import CryptoJS from 'crypto-js' 
 
const KEY = 'cb13dac810d178bf';
const IV = '84ced6979f6a3c40';


export interface CryptoType {
  encrypt: any
  decrypt: any
}
type WordArray = CryptoJS.lib.WordArray


class Crypto implements CryptoType { // 对称加密的形式
  key: WordArray; 
  iv: WordArray; 
  constructor(secretKey: string, secretKeyOffset: string){
    this.key = CryptoJS.enc.Utf8.parse(secretKey); // 十六位十六进制数作为密钥
    this.iv = CryptoJS.enc.Utf8.parse(secretKeyOffset) // 十六位十六进制数作为密钥偏移量
  }
 
  /* 加密 */
  encrypt (word: any) {
    const srcs = CryptoJS.enc.Utf8.parse(word)
    const encrypted = CryptoJS.AES.encrypt(srcs, this.key, { iv: this.iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 })
    return encrypted.ciphertext.toString().toUpperCase()
  }
 
  /* 解密 */
  decrypt (word: any) {
    const encryptedHexStr = CryptoJS.enc.Hex.parse(word)
    const srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr)
    const decrypt = CryptoJS.AES.decrypt(srcs, this.key, { iv: this.iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 })
    const decryptedStr = decrypt.toString(CryptoJS.enc.Utf8)
    return decryptedStr.toString()
  }
}

const MYCRYPTO = new Crypto('cb13dac810d178bf','84ced6979f6a3c40')

export default MYCRYPTO;